import { randomArray } from "@/utils";

/** 音效 */
export default function useSoundEffect(autoplay: boolean = true) {
	const rightMP3 = [
		"https://nx5.abc5g.com/res/amazing.mp3?v=1",
		"https://nx5.abc5g.com/res/good.mp3?v=1",
		"https://nx5.abc5g.com/res/great.mp3?v=1"
	];
	const errorMP3 = "https://nx5.abc5g.com/res/cuowu.mp3?v=2";
	
	const innerAudioContext = uni.createInnerAudioContext();
	innerAudioContext.autoplay = autoplay;
	/** 播放音效 */
	function playMp3(mp3: string){
		if (!mp3) return;
		if (mp3.includes('http')) {
			innerAudioContext.src = mp3;
		} else {
			innerAudioContext.src = 'https://nx5.abc5g.com/' + mp3;
		}
		innerAudioContext.play();
		innerAudioContext.onPlay(() => {
			
		});
		innerAudioContext.onError((res) => {
		  console.log(res.errMsg);
		  console.log(res.errCode);
		}); 
	}
	
	/** 播放成功的音效 */
	function palySuccessMp3() {
		randomArray(rightMP3);
		playMp3(rightMP3[0]);
	}
	
	/** 播放失败的音效 */
	function playFailMp3() {
		playMp3(errorMP3);
	}
	
	return {
		playMp3,
		palySuccessMp3,
		playFailMp3
	}
}